package cn.itcast.nems.studentrecord.converter;

import cn.itcast.nems.journal.dto.JournalRenewalDTO;
import cn.itcast.nems.order.dto.CreateOrderResultDTO;
import cn.itcast.nems.studentrecord.constant.ReferenceTypeEnum;
import cn.itcast.nems.studentrecord.dao.entity.JournalRenewalLine;
import cn.itcast.nems.studentrecord.dao.entity.JournalTransfer;
import cn.itcast.nems.studentrecord.dao.entity.JournalTransferLine;
import cn.itcast.nems.studentrecord.dao.entity.StudentRecord;
import cn.itcast.nems.studentrecord.dto.OrderPaidDTO;
import cn.itcast.nems.studentrecord.dto.change.StudentRecordChangeDTO;
import cn.itcast.nems.studentrecord.dto.change.StudentRecordJournalDTO;
import cn.itcast.nems.studentrecord.dto.repeat.JournalRepeatDTO;
import org.springframework.beans.BeanUtils;

/**
 * @author liyong
 * create: 2023-11-22 15:07:43
 **/
public class StudentRecordJournalConverter {

    public static StudentRecordJournalDTO orderSingup(StudentRecord studentRecord,
                                                      OrderPaidDTO orderPaid) {
        StudentRecordJournalDTO result = singup(studentRecord, orderPaid.getOrderId());
        result.setOrderId(orderPaid.getOrderId());
        final String orderLineId = orderPaid.getOrderLineIds().get(0);
        result.setOrderLineId(orderLineId);
        return result;
    }

    public static StudentRecordJournalDTO singup(StudentRecord studentRecord,
                                                 String orderId) {
        StudentRecordJournalDTO result = new StudentRecordJournalDTO();
        result.setJournalType(ReferenceTypeEnum.ORDER);
        result.setJournalId(orderId);
        result.setTargetClazzId(studentRecord.getClazzId());
        result.setTargetClazzName(studentRecord.getClazzName());
        result.setTargetClazzInDate(studentRecord.getInDate().toLocalDate());
        return result;
    }

    public static StudentRecordJournalDTO orderTransfer(OrderPaidDTO orderPaid,
                                                        JournalTransfer journal,
                                                        JournalTransferLine journalLine) {
        StudentRecordJournalDTO result = transfer(journal, journalLine);
        result.setOrderId(orderPaid.getOrderId());
        final String orderLineId = orderPaid.getOrderLineIds().get(0);
        result.setOrderLineId(orderLineId);
        return result;
    }

    public static StudentRecordJournalDTO transfer(JournalTransfer journal,
                                                   JournalTransferLine journalLine) {
        StudentRecordJournalDTO result = new StudentRecordJournalDTO();
        result.setJournalType(ReferenceTypeEnum.valueOf(journal.getType().name()));
        result.setJournalId(journal.getId());
        result.setJournalLineId(journalLine.getId());
        popPropertiesJournalTransfer(result, journal, journalLine);
        return result;
    }

    public static StudentRecordJournalDTO orderRepeat(OrderPaidDTO orderPaid,
                                                      JournalRepeatDTO journalRepeat) {
        StudentRecordJournalDTO result = repeat(journalRepeat);
        result.setOrderId(orderPaid.getOrderId());
        final String orderLineId = orderPaid.getOrderLineIds().get(0);
        result.setOrderLineId(orderLineId);
        return result;
    }

    public static StudentRecordJournalDTO repeat(JournalRepeatDTO journalRepeat) {
        StudentRecordJournalDTO result = new StudentRecordJournalDTO();
        result.setJournalType(ReferenceTypeEnum.REPEAT);
        result.setJournalId(journalRepeat.getId());
        result.setJournalLineId(journalRepeat.getId());
        result.setOriginStudentRecordId(journalRepeat.getOriginStudentRecordId());
        result.setOriginClazzId(journalRepeat.getOriginClazzId());
        result.setOriginClazzName(journalRepeat.getOriginClazzName());
        result.setOriginClazzOutDate(journalRepeat.getOriginClazzOutDate());
        result.setTargetStudentRecordId(journalRepeat.getStudentRecordId());
        result.setTargetClazzId(journalRepeat.getTargetClazzId());
        result.setTargetClazzName(journalRepeat.getTargetClazzName());
        result.setTargetClazzInDate(journalRepeat.getTargetClazzInDate());
        return result;
    }

    public static StudentRecordJournalDTO orderRenewal(OrderPaidDTO orderPaid,
                                                       JournalRenewalDTO journal,
                                                       JournalRenewalLine journalLine) {
        StudentRecordJournalDTO result = renewal(journal, journalLine);
        result.setOrderId(orderPaid.getOrderId());
        final String orderLineId = orderPaid.getOrderLineIds().get(0);
        result.setOrderLineId(orderLineId);
        return result;
    }

    public static StudentRecordJournalDTO renewal(JournalRenewalDTO journal,
                                                  JournalRenewalLine journalLine) {
        StudentRecordJournalDTO result = new StudentRecordJournalDTO();
        result.setJournalType(ReferenceTypeEnum.RENEWAL);
        result.setJournalId(journal.getId());
        result.setJournalLineId(journalLine.getId());
        popPropertiesJournalRenewal(result, journal, journalLine);
        return result;
    }

    public static StudentRecordJournalDTO createStudentRecordJournalDTO(StudentRecordChangeDTO studentRecordChange,
                                                                        CreateOrderResultDTO createOrderResultDTO) {
        StudentRecordJournalDTO result = new StudentRecordJournalDTO();
        BeanUtils.copyProperties(studentRecordChange, result);
        result.setOrderId(createOrderResultDTO.getOrderId());
        result.setOrderLineId(createOrderResultDTO.getOrderLineIds().get(0));
        return result;
    }

    private static void popPropertiesJournalRenewal(StudentRecordJournalDTO studentRecordJournalDTO,
                                                    JournalRenewalDTO journal,
                                                    JournalRenewalLine journalLine) {
        studentRecordJournalDTO.setOriginStudentRecordId(journalLine.getOriginStudentRecordId());
        studentRecordJournalDTO.setOriginClazzId(journalLine.getOriginClazzId());
        studentRecordJournalDTO.setOriginClazzName(journalLine.getOriginClazzName());
        studentRecordJournalDTO.setOriginClazzOutDate(journalLine.getOriginClazzOutDate());
        studentRecordJournalDTO.setTargetClazzId(journal.getTargetClazzId());
        studentRecordJournalDTO.setTargetClazzName(journal.getTargetClazzName());
        studentRecordJournalDTO.setTargetClazzInDate(journalLine.getTargetClazzInDate());
    }

    private static void popPropertiesJournalTransfer(StudentRecordJournalDTO studentRecordJournalDTO,
                                                     JournalTransfer journal,
                                                     JournalTransferLine journalLine) {
        studentRecordJournalDTO.setOriginStudentRecordId(journalLine.getOriginStudentRecordId());
        studentRecordJournalDTO.setOriginClazzId(journalLine.getOriginClazzId());
        studentRecordJournalDTO.setOriginClazzName(journalLine.getOriginClazzName());
        studentRecordJournalDTO.setOriginClazzOutDate(journalLine.getOriginClazzOutDate());
        studentRecordJournalDTO.setOriginClazzServiceDays(journalLine.getOriginClazzServiceDays());
        studentRecordJournalDTO.setOriginClazzSettlementDays(journalLine.getOriginClazzActualSettlementDays());
        studentRecordJournalDTO.setOriginClazzSettlementAmount(journalLine.getOriginClazzActualSettlementAmount());
        studentRecordJournalDTO.setTargetClazzId(journal.getTargetClazzId());
        studentRecordJournalDTO.setTargetClazzName(journal.getTargetClazzName());
        studentRecordJournalDTO.setTargetClazzInDate(journalLine.getTargetClazzInDate());
    }
}
